/**
 * 发现
 * 
 * @Author : chenglm
 * @Timestamp : 2016-12-26
 */
var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var rootview = ui("$");
var http = require("helper/http");
var mallUrl = require("url/mall");
var communityUtil = require("helper/community");
var imHelper = require("helper/im");
var bdmapHelper = require("helper/bdmap");

var listdata = mm("do_ListData");
var listView_list = ui("listView_list");
listView_list.bindItems(listdata);

var layoutHead = ui("layoutHead");
var mainHeadAdd = layoutHead.add("mainHead", "source://view/discover/header/mainHeader.ui", 0, 0);
var headerTitle = ui(mainHeadAdd+".headerTitle");
headerTitle.text = "圈货";

var communityId = null;
var pageNum = 1, totalPages = 1;
var dingweiX, dingweiY;
dingweiX=bdmapHelper.getDingwei().longitude;
dingweiY=bdmapHelper.getDingwei().latitude;
/**
 * 排序方式：sales-销量;distance-距离
 */
var sortType ="sales";//默认 销量排序
function init() {
	deviceone.print(dingweiX, "dingweiX");
	deviceone.print(dingweiY, "dingweiY");
	pageNum = 1;
	communityId = communityUtil.communityVisitId();
	var param ={"communityId":communityId, "x":dingweiX, "y":dingweiY, "sortType":sortType};
	http.post(mallUrl.url.shop.discover, param, function(data) {
		listView_list.rebound();
		listdata.removeAll();
		deviceone.print(JSON.stringify(data.adList));
		listdata.addData([{"template":4,"adList":data.adList}]);
		listdata.addData([{"template":1}]);
		listdata.addData([{"template":2,"special":data.productList}]);
		listdata.addData([{"template":3,"gridNeiShop":data.neiShopList}]);
		if (data.collectShops != null) {
			listdata.addData(data.collectShops);
		}
		listdata.addData(data.comShopPage.content);
		if (data.comShopPage.content != null) {
			totalPages = data.comShopPage.totalPages;
		}
		listView_list.refreshItems();
	});
}

function getNextPageData(){
	pageNum++;
	if (pageNum <= totalPages) {
		//var param = {page:pageNum,communityId:communityId};
		var param ={page:pageNum,"communityId":communityId, "x":dingweiX, "y":dingweiY, "sortType":sortType};
		http.post(mallUrl.url.shop.discoverList, param, function(data) {
			totalPages = data.comShopPage.totalPages;
			listdata.addData(data.comShopPage.content);
			listView_list.rebound();
			listView_list.refreshItems();
		})
	} else {
		nf.toast("已是最后一页");
		listView_list.rebound();
	}
}

listView_list.on("pull", function(data){
	//其中state=0：表示开始下拉headerview，；state=1：表示下拉headerview超过headerview的高度，触发一次这个事件；state=2：下拉超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){
		init();
	}
});
//上拉列表，翻页数据
listView_list.on("push", function(data){
	//其中state=0：表示开始上推headerview，；state=1：表示上推headerview超过headerview的高度，触发一次这个事件；state=2：上推超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){	
		getNextPageData();
	}
});
page.on("index_discover_tab_touch", function(data) {
		init();
});

doSomeThing2MsgTip();
//处理消息红点是否提示
function doSomeThing2MsgTip(){
	var unreadCount = imHelper.getMyUnReadCount();
	//deviceone.print(unreadCount,"unreadCount-未读消息数量");
	if(parseInt(unreadCount) > 0){
		app.fire("qq_fire_show_msg_tip");//显示消息红点
	}else{
		app.fire("qq_fire_hide_msg_tip");//隐藏消息红点
	}
}
page.on("discover_neiShop_list_sort", function(data) {
	if(data && data.sortType=="sales"){//按销量查询周边商家
		//nf.toast("销量");
		sortType ="sales";
		reSortComShopList();
	}else if(data && data.sortType=="distance"){//按距离查询周边商家
		//nf.toast("距离");
		sortType ="distance";
		reSortComShopList();
	}
})

//重新按新的排序 查询周边商家
function reSortComShopList() {
	var indexArray = [];
	for (var i = 0; i < listdata.getCount(); i++) {
		oneData = listdata.getOne(i);
		
		if (oneData.template==0 || oneData.template==undefined
				|| oneData.template=="undefined") {//周边商家
			indexArray.push(i);
		}
	}
	if(indexArray.length>0){
		listdata.removeData(indexArray);
		listView_list.refreshItems();
	}
	pageNum=1;
	var param ={page:pageNum,"communityId":communityId, "x":dingweiX, "y":dingweiY, "sortType":sortType};
	http.post(mallUrl.url.shop.discoverList, param, function(data) {
		totalPages = data.comShopPage.totalPages;
		listdata.addData(data.comShopPage.content);
		listView_list.rebound();
		listView_list.refreshItems();
	})
	if(listdata.getCount()>0){//滚动到
		listView_list.scrollToPosition(listdata.getCount()-1, false);
	}
}